import numpy as np
import matplotlib.pyplot as plt
from PIL import Image

# 读取图像并转为灰度
img = Image.open('D:\\2D-DFT\image\\test.jpg').convert('L')
img_np = np.array(img)

# 做2D傅里叶变换
f = np.fft.fft2(img_np)
fshift = np.fft.fftshift(f)

# 计算相位谱（弧度）
phase = np.angle(fshift)

# 映射到 [0, 1] 以便可视化
norm_phase = (phase + np.pi) / (2 * np.pi)

# 显示
plt.figure(figsize=(8, 4))
plt.imshow(norm_phase, cmap='gray')
plt.title('Phase Spectrum')
plt.axis('off')
plt.show()
